package algorithm.array;

import java.util.*;

/**
 * 两个数组的交集
 *
 * 输入：nums1 = [1,2,2,1], nums2 = [2,2]
 * 输出：[2]
 *
 * 输入：nums1 = [4,9,5], nums2 = [9,4,9,8,4]
 * 输出：[9,4]
 */
public class Intersection {

    public static void main(String[] args) {

        int [] arr1 = {4,9,5};
        int [] arr2 = {9,4,9,8,4};
        System.out.println(Arrays.toString(intersection(arr1, arr2)));
    }

    public static int[] intersection(int[] nums1, int[] nums2) {

        Map map = new HashMap<Integer, Integer>();
        Set<Integer> list = new HashSet<>();

        for(int a : nums1){
            map.put(a, a);
        }

        for (int i = 0; i < nums2.length; i++) {
            if (map.containsKey(nums2[i])){
                list.add(nums2[i]);
            }
            continue;
        }

        Integer [] integers = list.toArray(new Integer[list.size()]);

        int[] ints = Arrays.stream(integers).mapToInt(Integer::valueOf).toArray();
        return ints;
    }
}
